package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class FriendshipPeer extends BasePeer{
	public static final String tableName = "Friendship";
	public static final String USERID = "userId";
	public static final String FRIENDID = "friendId";
	public static final String RELATIONSHIP = "relationship";
	public static final String[] columnNames=new String[]{"userId","friendId","relationship"};
	public static Friendship getFriendshipByPKs(Object friendId,Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(FriendshipPeer.tableName,FriendshipPeer.columnNames," TRUE  AND `friendId`=?  AND `userId`=? ",new Object[]{friendId,userId,},0, 1,conn);
			Friendship friendship = new Friendship();
		fill(rs,friendship);
		return friendship;
	}
	public static Friendship getFriendshipReferenceColumnsOnlyByPKs(Object friendId,Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(FriendshipPeer.tableName,new String[]{"friendId","userId",}," TRUE AND `friendId`=?AND `userId`=?",new Object[]{friendId,userId,}, 0,  1,conn);
			Friendship friendship = new Friendship();
		fill(rs,friendship);
		return friendship;
	}
	public static Friendship getFriendshipByUserId(long userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(FriendshipPeer.tableName,FriendshipPeer.columnNames,"`userId`=?",new Object[]{userId},0, 1,conn);
			Friendship friendship = new Friendship();
		fill(rs,friendship);
		return friendship;
	}
	public static Friendship getFriendshipByFriendId(long friendId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(FriendshipPeer.tableName,FriendshipPeer.columnNames,"`friendId`=?",new Object[]{friendId},0, 1,conn);
			Friendship friendship = new Friendship();
		fill(rs,friendship);
		return friendship;
	}
	public static void fill(ResultSet rs, Friendship friendship) throws SQLException {
		if(rs.next()){
			try{friendship.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{friendship.setFriendId(rs.getLong("friendId"));}catch(Exception ex){}
			try{friendship.setRelationship(rs.getInt("relationship"));}catch(Exception ex){}
			friendship.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<Friendship> friendshipCollection) throws SQLException {
		while(rs.next()){
			Friendship friendship=new Friendship();
			try{friendship.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{friendship.setFriendId(rs.getLong("friendId"));}catch(Exception ex){}
			try{friendship.setRelationship(rs.getInt("relationship"));}catch(Exception ex){}
			friendship.resetModified();
			friendshipCollection.add(friendship);
		}
	}
	public static boolean update(Friendship friendship, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < FriendshipPeer.columnNames.length; i++) {
			if (((FriendshipBase)friendship).modified[i]) {
				columns.add(FriendshipPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `Friendship` SET ");
		sql.append("WHERE TRUE AND `friendId` = ? AND `userId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,friendship.get(column));
		}
		pstmt.setObject(columns.size()+1+0,friendship.get("friendId"));
		pstmt.setObject(columns.size()+1+1,friendship.get("userId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(Friendship friendship, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (friendship.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `Friendship` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (friendship.get(column) != null) {
				pstmt.setObject(i + 1 + j, friendship.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<Friendship> getFriendshipCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<Friendship> collection=new Vector<Friendship>();
		fill(rs,collection);
		return collection;
	}
}
